package org.ladybug.service.auth;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.ladybug.dao.DAOService;
import org.ladybug.dao.persistence.Ruolo;
import org.ladybug.dao.persistence.RuoloUtente;
import org.ladybug.dao.persistence.Utente;
import org.ladybug.service.admin.AdminService;
import org.ladybug.service.auth.LadyBugUserDetails.LadybugGrantAuthority;
import org.springframework.dao.DataAccessException;
import org.springframework.security.BadCredentialsException;
import org.springframework.security.userdetails.UserDetails;
import org.springframework.security.userdetails.UserDetailsService;
import org.springframework.security.userdetails.UsernameNotFoundException;

public class HibernateAuthenticationService implements UserDetailsService {

	private DAOService daoService;
	private AdminService adminService ;

	public UserDetails loadUserByUsername(String username)
			throws UsernameNotFoundException, DataAccessException {
		System.out.println("-------------------------------------------------");
		Utente utente = new Utente();
		utente.setNickname(username);
		utente = getUtenteByCredential(utente);
		if (utente == null) {
			throw new BadCredentialsException("Username errato");
		}
		LadyBugUserDetails userDetails = new LadyBugUserDetails();
		userDetails.setUtente(utente);
		Set ruolos = utente.getRuoloUtentes();
		LadybugGrantAuthority[] authorities = new LadybugGrantAuthority[ruolos.size()];
		Iterator it = ruolos.iterator();
		for (int i=0;it.hasNext();i++) {
			Ruolo ruolo = ((RuoloUtente) it.next()).getRuolo();
			ruolo = adminService.findRuoloByPK(ruolo);
			System.out.println(ruolo.getDescrizione());
			authorities[i]=new LadyBugUserDetails().new LadybugGrantAuthority(ruolo);
			userDetails.addAuthority(authorities[i]);
		}

		return userDetails;
	}

	private Utente getUtenteByCredential(Utente utente) {
		// byte[] keyPassword = new
		// StringBuffer(utente.getNickname()).append(utente.getPassword()).toString().getBytes();
		// keyPassword = MessageDigest.getInstance("SHA").digest(keyPassword);
		// utente.setPassword(HexReader.bytesToHex(keyPassword));
		DetachedCriteria criteria = DetachedCriteria.forClass(Utente.class);
		Criterion crUser = Expression.eq("nickname", utente.getNickname());
		criteria.add(crUser);
		// Criterion crPwd = Expression.eq("password", utente.getPassword());
		// Criterion and = Expression.and(crUser, crPwd);
		// criteria.add(and);
		List result = daoService.findByCriteria(criteria);
		System.out
				.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		System.out.println(result);
		System.out
				.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
		if (result.size() == 0) {
			return null;
		}
		utente = (Utente) result.get(0);

		return utente;

	}

	// --------------------------- set e get


	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	public DAOService getDaoService() {
		return daoService;
	}

	public void setDaoService(DAOService daoService) {
		this.daoService = daoService;
	}

}
